//
//  LXJFansViewModel.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/4/9.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit

class LXJFansViewModel: NSObject {
    /// 数据源属性
    var fansModelList: [LXJFansDetailsViewModel]?
    
    
    /// 请求粉丝列表数据
    func requestFansInfo(success: ((Bool) -> ())?) -> Void {
        /// 发起网络请求
        LXJNetworkManager.sharedManager.requestWeiboFansInfo(access_token: LXJOAuthViewModel.sharedOAuthViewModel.accessToken!, userID: (LXJOAuthViewModel.sharedOAuthViewModel.userAccount?.uid)!) { (response, error) in
            /// 数据请求失败
            if error != nil{
                print("微博首页网络数据请求失败,时报原因如下:\(error!)")
                /// 请求后的闭包回调
                success!(false)
                return
            }
            
            /// 执行到此请求成功
            guard let dic = response as? [String: Any] else{
                print("微博首页请求数据返回的数据不是标准数据")
                /// 请求后的闭包回调
                success!(false)
                return
            }
            
            ///  执行到此应是一个标准的字典,并完成字典转模型
            let fansList = dic["users"] as! [[String: Any]]
            
            /// 模型转换
            let fansInfoModel = NSArray.yy_modelArray(with: LXJFansInfoModel.self, json: fansList) as! [LXJFansInfoModel]
            
            /// 遍历fansInfoModel,关联LXJFansDetailsViewModel
            var tempARR: [LXJFansDetailsViewModel] = [LXJFansDetailsViewModel]()
            for fansModel in fansInfoModel{
                let fansViewModel = LXJFansDetailsViewModel()
                fansViewModel.fansDetails = fansModel
                tempARR.append(fansViewModel)
            }
            /// 属性关联
            self.fansModelList = tempARR
            
            success!(true)
        }
    }
}
